<?php

namespace App\Http\Controllers\Shuzhai;

//use Illuminate\Http\Request;
use Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Book;
use App\Models\Shuzhai;
use App\Models\Shuzhai_dianzan;
use App\Models\Shuzhai_pinglun;
use App\Models\UsersReg;

use Session;
use Redirect;

class ShuzhaiController extends Controller
{
    public function lists()
    {
        //书摘首页
        $shuzhailists = Shuzhai::orderBy('add_time', 'desc')->paginate(10);
        foreach($shuzhailists as $shuzhailist)
        {
            $imglist = $shuzhailist->imglist;
            if($imglist != null)
            { //如果有图片                
                $imglist_arr = explode(',' , $imglist);
                //取前三个数组，最多显示三个图片
                $imglist_arr3 = array_slice($imglist_arr,0,3); 
                //图片数组
                $shuzhailist['imglist'] = $imglist_arr3; 
                //图片的总数量
                $shuzhailist['imgcount'] = count($imglist_arr);                
            }
            else
            {
                //没有图片
                $shuzhailist['imgcount'] = 0;
            }
            //对应的会员资料
            $shuzhailist['users'] = $shuzhailist->hasuserinfo;   
            //对应的书籍资料
            $shuzhailist['books'] = Book::select('book_name')->where('book_id', $shuzhailist->bookid)->first();
            //显示所有图片
            $shuzhailist['PhotoSwipe'] = $imglist;
            //评论数量
            $shuzhailist['pingluncount'] = Shuzhai_pinglun::where('shuzhai_id', $shuzhailist->id)->count();
            //点赞数量
            $shuzhailist['dianzan'] = Shuzhai_dianzan::where('shuzhai_id', $shuzhailist->id)->count();
        }
        //dd($shuzhailists);

        return view('shuzhai/lists',array(
            'shuzhailists' => $shuzhailists
        ));
    }
    public function shuzhailist($bookid)
    {
        //书摘列表
        $books = Book::where('book_id' , $bookid)->first();
        if($books == '')
        {
            abort('404');
        }
        $shuzhailists = Shuzhai::where('bookid' , $bookid)->orderBy('add_time', 'desc')->get();
        foreach($shuzhailists as $shuzhailist)
        {
            $imglist = $shuzhailist->imglist;
            if($imglist != null)
            { //如果有图片                
                $imglist_arr = explode(',' , $imglist);
                //取前三个数组，最多显示三个图片
                $imglist_arr3 = array_slice($imglist_arr,0,3); 
                //图片数组
                $shuzhailist['imglist'] = $imglist_arr3; 
                //图片的总数量
                $shuzhailist['imgcount'] = count($imglist_arr);                
            }
            else
            {
                //没有图片
                $shuzhailist['imgcount'] = 0;
            }
            

            //对应的会员资料
            $shuzhailist['users'] = $shuzhailist->hasuserinfo;
             
        }
        ///dd($shuzhailists);

        

        return view('shuzhai/shuzhailist',array(
            'shuzhailists' => $shuzhailists,
            'books' => $books,
            'bookid' => $bookid
        ));
    }

    public function pubshuzhai($bookid)
    {
        //发布书摘
        $books = Book::where('book_id' , $bookid)->first();
        return view('shuzhai/pubshuzhai',array(
            'books' => $books,
            'bookid' => $bookid
        ));
    }

    public function pubshuzhai_post()
    {
        //保存发布书摘 

        /****单个图片上传****/
        // $file = $_FILES['file']['name'];
        // if($file)
        // {
        //     //建立文件夹
        //     $dir = iconv("UTF-8", "GBK", "shuzhai_img");
        //     if (!file_exists($dir)){
        //         mkdir ($dir, 0777, true);
        //     }
        //     //文件的路径
        //     $file_path = $dir . '/' . $_FILES['file']['name'];
        //     move_uploaded_file($_FILES['file']['tmp_name'], $file_path);
        // }
        /****单个图片上传end****/

        $shuzhai = new Shuzhai;

        // 获得会员id
        $nameid = Session::get('nameid');
        $shuzhai->uid = $nameid;

        // 得到书籍的bookid
        $bookid = $_POST['bookid'];
        $shuzhai->bookid = $bookid; 

        // 获得书摘内容
        $pubshuzhai = $_POST['pubshuzhai'];
        $shuzhai->content = $pubshuzhai;

        // 获得想法内容
        $pubxiangfa = $_POST['pubxiangfa'];
        $shuzhai->xiangfa = $pubxiangfa;

        if($_POST['flists'] != '') //有选择图片
        {
            //建立文件夹
            $dir = iconv("UTF-8", "GBK", "shuzhai_img"); //存放图片目录shuzhai_img
            if (!file_exists($dir)){
                mkdir ($dir, 0777, true);
            }         

            $arrpath = [];
            //获得上传的图片
            $fileslists = $_FILES['fileslist'];
            for($i=0; $i<count($fileslists['name']); $i++) {
                $tmpFile_Path = $fileslists['tmp_name'][$i];
                if ($tmpFile_Path != ""){
                    //文件名前缀
                    $newfile_prv = 'shuzhai_' .time() . str_random(10);
                    //文件扩展名字
                    $pathinfo = pathinfo($fileslists['name'][$i]);
                    $extension = $pathinfo['extension'];
                    $newFile_Path = $dir . '/' . $newfile_prv . '.' . $extension;  //保存文件使用                    
                    move_uploaded_file($tmpFile_Path, $newFile_Path);
                    //保存到数据库里的文件，不带目录
                    $FPath = $newfile_prv . '.' . $extension;
                    //拼接图片名字，用逗号隔开
                    array_push($arrpath, $FPath);
                }
            }
            //数组转成以为逗号分割的字符串
            $shuzhai->imglist = implode(',' , $arrpath);
        }
        
        $shuzhai->add_time = time();
        $shuzhai->save(); //保存书摘

        //上传成功
        return 1; 
    }

    public function pubshuzhai_pinglun($id)
    {
        //发布书摘评论页面
        return view('shuzhai/pubshuzhai_pinglun',array(
            'id' => $id
        ));
    }

    public function pubshuzhai_pinglun_post()
    {
        //保存发布的书摘评论
        $shuzhaiid = request::input('shuzhaiid');
        $content = request::input('content');

        $shuzhai_pinglun = new Shuzhai_pinglun;
        $shuzhai_pinglun->uid = Session::get('nameid');
        $shuzhai_pinglun->shuzhai_id = $shuzhaiid;
        $shuzhai_pinglun->content = $content;
        $shuzhai_pinglun->add_time = time();
        $shuzhai_pinglun->save();

        return 'ok';
    }

    public function shuzhaipinglun($id)
    {
        //书摘的评论
        $shuzhailist = Shuzhai::find($id);
        //阅读次数增加
        $shuzhailist->click = $shuzhailist->click + 1;
        $shuzhailist->save();
        
        $books = Book::where('book_id' , $shuzhailist->bookid)->first();
        if($books == '')
        {
            abort('404');
        }

        $imglist = $shuzhailist->imglist;
        if($imglist != null)
        { 
            //图片数组               
            $imglist_arr = explode(',' , $imglist);             
        }
        else
        {
            //没有图片
            $imglist_arr = 0;
        }            

        //对应的会员资料
        $users = UsersReg::find($shuzhailist->uid);

        //对应的评论
        $shuzhai_pingluns = Shuzhai_pinglun::where('shuzhai_id', $id)->orderBy('add_time', 'desc')->get();   
        foreach($shuzhai_pingluns as $shuzhai_pinglun)
        {
            //评论对应的会员资料
            $shuzhai_pinglun['users'] = $shuzhai_pinglun->hasuserinfo;
        }

        //对应的点赞数量
        $dianzan_count = Shuzhai_dianzan::where('shuzhai_id', $id)->count();

        return view('shuzhai/shuzhaipinglun',array(
            'shuzhailist' => $shuzhailist,
            'books' => $books,
            'imglist_arr' => $imglist_arr,
            'imglist' => $imglist,
            'users'  => $users,
            'id'   =>  $id,
            'shuzhai_pingluns' => $shuzhai_pingluns,
            'dianzan_count' => $dianzan_count
        ));
    }

    public function dianzan()
    {
        //点赞
        $nameid = Session::get('nameid');
        $shuzhai_id = request::input('shuzhaiid');
        if(!$nameid)
        {
            //没有登录
            return 1;
        }

        $shuzhai_dianzan = Shuzhai_dianzan::where('uid', $nameid)->where('shuzhai_id', $shuzhai_id)->first(); 
        if($shuzhai_dianzan == null)
        {        
            //记录点赞用户
            $dianzan = new Shuzhai_dianzan;
            $dianzan->shuzhai_id = $shuzhai_id;
            $dianzan->uid = $nameid;
            $dianzan->save();
            return 2;
        }
        else
        {
            //已经点过赞了
            return 3;
        }
    }

}
